package com.linkage.bss.crm.packagemgr.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linkage.bss.commons.dao.ibatis.BaseDAO;
import com.linkage.bss.crm.packagemgr.dao.IPackageObjDAO;
import com.linkage.bss.crm.packagemgr.model.ActionOrderItem;
import com.linkage.bss.crm.packagemgr.model.ActionParameter;
import com.linkage.bss.crm.packagemgr.model.OfferRoles;
import com.linkage.bss.crm.packagemgr.model.OfferSpecDto;
import com.linkage.bss.crm.packagemgr.model.PackageObject;
import com.linkage.bss.crm.packagemgr.model.ParameterActionRela;
import com.linkage.bss.crm.packagemgr.model.ParameterMapRela;
import com.linkage.bss.crm.packagemgr.model.RoleObj;
import com.linkage.bss.crm.packagemgr.util.Constant;

/**
 * 包对象dao 接口
 *
 * @author  Administrator
 * @version  [版本号, 2013-5-9]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PackageObjDAOImpl extends BaseDAO implements IPackageObjDAO {
	Logger log = LoggerFactory.getLogger(PackageObjDAOImpl.class);

	public List<ActionOrderItem> queryActionOrderItemByPackId(Long packageId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("packageId", packageId);
		return getSqlMapClientTemplate().queryForList("PackageObj.queryActionOrderItemByCond", map);
	}

	public List<ActionOrderItem> queryActionOrderItemByParams(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryActionOrderItemByCond", params);
	}

	public List<PackageObject> queryPackgeObject(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryPackgeObject", params);
	}

	public List<OfferRoles> queryOfferRoles(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryOfferRoles", params);
	}

	public List<RoleObj> queryRoleObjs(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryRoleObjs", params);
	}

	public List<Map<String, Object>> queryServSpecRela(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryServSpecRela", params);
	}

	public List<String> queryOfferExcludeInfo(String offerSpecId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryOfferExcludeInfo", offerSpecId);
	}

	public List<Map<String, String>> queryOfferRelaInfo(String offerSpecId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryOfferRelaInfo", offerSpecId);
	}

	public List<Map<String, String>> querySelectOfferRelaInfo(String offerSpecId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.querySelectOfferRelaInfo", offerSpecId);
	}

	public Long queryRelaActionItemCount(Map<String, Object> params) {
		return (Long) getSqlMapClientTemplate().queryForObject("PackageObj.queryRelaActionItemCount", params);
	}

	public Long queryAvailableOfferObjCount(Map<String, Object> params) {
		return (Long) getSqlMapClientTemplate().queryForObject("PackageObj.queryAvailableOfferObjCount", params);
	}

	public Long queryAvailableProdObjCount(Map<String, Object> params) {
		return (Long) getSqlMapClientTemplate().queryForObject("PackageObj.queryAvailableProdObjCount", params);
	}

	public Long queryActonOrderItemCount(Map<String, Object> params) {
		return (Long) getSqlMapClientTemplate().queryForObject("PackageObj.queryActonOrderItemCount", params);
	}

	public List<String> queryRelatedOffers(String offerSpecId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryRelatedOffers", offerSpecId);
	}

	public String ifCompOffer(String offerSpecId) {
		return (String) getSqlMapClientTemplate().queryForObject("PackageObj.ifCompOffer", offerSpecId);
	}

	public List<ActionParameter> getActionParameterByAID(Long actionItemId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.getActionParameterByAID", actionItemId);
	}

	public void insertActionItem(ActionOrderItem item) {
		getSqlMapClientTemplate().insert("PackageObj.insertActionItem", item);

	}

	public void insertPackageObj(PackageObject obj) {
		getSqlMapClientTemplate().insert("PackageObj.insertPackageObj", obj);

	}

	public void insertActionParameter(ActionParameter param) {
		getSqlMapClientTemplate().insert("PackageObj.insertActionParam", param);
	}

	public void insertParameterActionRela(ParameterActionRela parameterActionRela) {
		getSqlMapClientTemplate().insert("PackageObj.insertParameterActionRela", parameterActionRela);
	}

	public void insertParameterMapRela(Long packageParamID, Long actionParamID) {
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("packageParameterId", packageParamID);
		map.put("actionParameterId", actionParamID);
		getSqlMapClientTemplate().insert("PackageObj.insertParameterMapRela", map);

	}

	public void updatePackageObj(PackageObject obj) {
		getSqlMapClientTemplate().update("PackageObj.updatePackageobj", obj);

	}

	public void updateActionOrderItem(ActionOrderItem actionOrderItem) {
		getSqlMapClientTemplate().update("PackageObj.updateActionOrderItem", actionOrderItem);
	}

	public void deleteActionOrderItem(ActionOrderItem actionOrderItem) {
		getSqlMapClientTemplate().delete("PackageObj.deleteActionOrderItem", actionOrderItem);
	}

	public void deleteActionParam(String actionItemId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionParam", Long.valueOf(actionItemId));

	}

	public void deleteParamMap(Long paramId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionParamMap", paramId);
	}

	public void deleteParamRela(Long paramId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionParamRela", paramId);
	}

	public void deleteActionOrderItem(Long itemId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionOrderItem", itemId);

	}

	/**
	 * 删除参数决定条目
	 * @param packageId [参数说明]
	 *
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public void delParameterActionRelaByPackId(Long packageId) {
		getSqlMapClientTemplate().delete("PackageObj.delParameterActionRelaByPackId", packageId);
	}

	/**
	 *删除动作参数
	 *
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public void delActionParameterByPackId(Long packageId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionParameterByPackId", packageId);
	}

	/**
	 * 删除动作
	 * @param packageId [参数说明]
	 *
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public void delActionOrderItemByPackId(Long packageId) {
		getSqlMapClientTemplate().delete("PackageObj.delActionOrderItemByPackId", packageId);
	}

	public void deletePackgeObj(Long packageId, String nbr) {
		Map map = new HashMap();
		map.put("packageId", packageId);
		map.put("objectNumber", nbr);
		getSqlMapClientTemplate().delete("PackageObj.delPackgeObj", map);

	}

	/**
	 * 删除包参数与条目参数关联关系
	 * @param packageId [参数说明]
	 * 
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public void delParameterMapRelaByPackId(Long packageId) {
		getSqlMapClientTemplate().delete("PackageObj.delParameterMapRelaByPackId", packageId);
	}

	/**
	 * 删除包参数
	 * @param packageId [参数说明]
	 * 
	 * @return void [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public void delPackageParameterByPackId(Long packageId) {
		getSqlMapClientTemplate().delete("PackageObj.delPackageParameterByPackId", packageId);
	}

	public List<ParameterActionRela> queryParamActionRelaByParamId(Long paramId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.queryParamActionRelaByParamId", paramId);
	}

	public ParameterMapRela queryParamMapRelaByParamId(Long paramId) {
		return (ParameterMapRela) getSqlMapClientTemplate().queryForObject("PackageObj.queryParamMapRelaByParamId",
						paramId);
	}

	/* (non-Javadoc)
	 * @see com.linkage.bss.crm.pack.query.IPackQueryDAO#queryParameterActionRelasByPackId(java.lang.Long)
	 */
	public List<ParameterActionRela> queryParameterActionRelasByPackId(Long packId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.selectByPackId", packId);
	}

	public List<ParameterMapRela> queryParameterMapRelasByPackId(Long packId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.selectMapRelaMapByPackId", packId);
	}

	public List<OfferSpecDto> queryCoreOfferByObj(Map<String, Object> param) {
		List<OfferSpecDto> offerSpecs = new ArrayList<OfferSpecDto>();
		Long curOfferSpecId = new Long(param.get("offerSpecId").toString());
		param.put("notSubscribeIS", Constant.ITEM_SPEC_CANNOT_SINGLE_SUBSCRIBE);
		param.put("osAttrClass", Constant.ITEM_SPEC_OFFER_SPEC_ATTR_CLASS);
		//判断curOfferSpecId 的 agreementType =3 是协议时，先找关联的销售品。
		Map<String, String> curOfferSpec = (Map<String, String>) getSqlMapClientTemplate().queryForObject(
						"PackageObj.queryOfferSpecById", curOfferSpecId);
		System.out.println(curOfferSpec);
		Integer agreementTypeCd = Integer.valueOf(curOfferSpec.get("agreementTypeCd"));
		param.put("agreementTypeCd", agreementTypeCd);
		if (Constant.CONST_AGREEMENT_TYPE_AGREEMENT.equals(agreementTypeCd)) {
			//找连带的销售品
			List<OfferSpecDto> reqOfferSpecList = getSqlMapClientTemplate().queryForList("PackageObj.getReqOfferSpec",
							param);
			log.debug("ReqOfferSpecList size:{}", reqOfferSpecList.size());
			if (reqOfferSpecList.size() > 0) {
				offerSpecs.addAll(reqOfferSpecList);
			}
			log.debug("ReqOfferSpecList end size:{}", offerSpecs.size());
		}
		if (offerSpecs.size() == 0) {
			//找依赖的销售品
			param.put("relaTypeCd", Constant.RELA_TYPE_RELY);
			List<OfferSpecDto> relaedOfferSpecList = getSqlMapClientTemplate().queryForList(
							"PackageObj.getRelaedOfferSpec", param);
			log.debug("relaedOfferSpecList size:{}", relaedOfferSpecList.size());
			for (OfferSpecDto os : relaedOfferSpecList) {
				param.put("offerSpecId", os.getOfferSpecId());//重写OFFER_SPEC_ID
				offerSpecs.add(os);
				//				if (ifOfferSpecInCondDim(param)) {
				//					offerSpecs.add(os);
				//				}
			}
			log.debug("relaedOfferSpecList end size:{}", offerSpecs.size());
			if (offerSpecs.size() == 0) {
				//查找排除互斥后的销售品
				param.put("offerSpecId", curOfferSpecId);
				List<OfferSpecDto> unExclusiveOfferSpec = getSqlMapClientTemplate().queryForList(
								"PackageObj.getUnExclusiveOfferSpec", param);
				//				offerSpecs = new ArrayList<OfferSpecDto>();
				log.debug("unExclusiveOfferSpec size:{}", unExclusiveOfferSpec.size());
				for (OfferSpecDto os : unExclusiveOfferSpec) {
					param.put("offerSpecId", os.getOfferSpecId());//重写OFFER_SPEC_ID
					offerSpecs.add(os);
					//					if (ifOfferSpecInCondDim(param)) {
					//						offerSpecs.add(os);
					//					}
				}
				log.debug("unExclusiveOfferSpec end size:{}", offerSpecs.size());
			}
		}
		return offerSpecs;
	}

	public boolean ifHasBaseOfferCfg(Map<String, Object> params) {
		Long result = (Long) getSqlMapClientTemplate().queryForObject("PackageObj.ifHasBaseOfferCfg", params);
		return result.longValue() > 0 ? true : false;
	}

	public List<RoleObj> getSameFeeTypeRoleListByOfferSpecId(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.getSameFeeTypeRoleListByOfferSpecId", params);
	}

	public boolean isOfferExclude(String offerSpecId, String destOfferSpecId) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("offerSpecId", offerSpecId);
		params.put("destOfferSpecId", destOfferSpecId);
		Long result = (Long) getSqlMapClientTemplate().queryForObject("PackageObj.isOfferExclude", params);
		return result.longValue() > 0 ? true : false;
	}

	public boolean ifOfferIsMainOffer(String offerSpecId) {
		Long result = (Long) getSqlMapClientTemplate().queryForObject("PackageObj.ifOfferIsMainOffer", offerSpecId);
		return result.longValue() > 0 ? true : false;
	}

	public OfferSpecDto queryOfferSpecByOfferSpecId(String offerSpecId) {
		return (OfferSpecDto) getSqlMapClientTemplate().queryForObject("PackageObj.queryOfferSpecByOfferSpecId",
						offerSpecId);
	}

	public List<Map> getOfferMemRelaGrps(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList("PackageObj.getOfferMemRelaGrps", params);
	}

	public List<Map> getOfferGrpMemsByGrpId(String relaGrpId) {
		return getSqlMapClientTemplate().queryForList("PackageObj.getOfferGrpMemsByGrpId", relaGrpId);
	}
}
